﻿using SharPy.GameClasses;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace SharPy.Parser.clCommands
{
    class ShowText : clCommand
    {
        public static bool RegExpCheck(string s)
        {
            return Regex.IsMatch(s, @"(\w*)*\s+("".*"")");
        }

        public static clCommand Parse(string s)
        {
            MatchCollection m = Regex.Matches(s, @"(.*)\s+""(.*)""");

            s = clText.cleanString(s);

            if (clCharacters.sLabel.Contains(m[0].Groups[1].Value.Trim()))
            {
                //NVL mode
                if (clCharacters.checkNVL(m[0].Groups[1].Value.Trim()) == true)
                    return new clCommand(clCommand.Name.ShowNVL
                                            , m[0].Groups[2].Value.Trim()
                                            , clCharacters.getColor(m[0].Groups[1].Value.Trim())
                            );

                else
                    return new clCommand(clCommand.Name.ShowText
                                            , m[0].Groups[2].Value.Trim()
                                            , clCharacters.getColor(m[0].Groups[1].Value.Trim())
                                            , clCharacters.getName(m[0].Groups[1].Value.Trim())
                            );
            }
            else
                return new clCommand(clCommand.Name.ShowText
                                        , m[0].Groups[2].Value.Trim()
                                        , clCharacters.getColor(null)
                                        , m[0].Groups[1].Value.Trim());
        }

        public static async Task<int> CommandHandle(clCommand objCom, GamePage page) 
        {
            TextBlock sMainText = (TextBlock)page.FindName("sMainText");
            TextBlock sHeaderText = (TextBlock)page.FindName("sHeaderText");

            if (objCom.sParameter[1] != null)
            {
                byte R = Convert.ToByte(objCom.sParameter[1].Substring(1, 2), 16);
                byte G = Convert.ToByte(objCom.sParameter[1].Substring(3, 2), 16);
                byte B = Convert.ToByte(objCom.sParameter[1].Substring(5, 2), 16);
                sHeaderText.Foreground = new SolidColorBrush(Color.FromArgb(R, G, B, 0));

                sHeaderText.Text = objCom.sParameter[2];
                sMainText.Text = clText.cleanString(objCom.sParameter[0]);
            }
            else
            {
                sHeaderText.Foreground = new SolidColorBrush(page.defaultTextColor);
                sHeaderText.Text = clText.cleanString(objCom.sParameter[0]);
                sMainText.Text = "";
            }
            clText.markRead(page.CurrentGame.LineNoHolder);
            return 1;
        }
    }
}
